package com.happystudy.util;

import com.happpystudy.model.course.CourseKey;
import com.happpystudy.model.course.CourseTeacherKey;
import com.happpystudy.model.course.CourseTopic;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;

public class KeyUtil {
    public static final String CURRENT_USER_PRI_KEY="CURRENT_USER";
    public static final String USER_KEY = "WEBUSER";
    public static final String COURSE_KEY="COURSE_KEY";
    public static final String COURSE_TEACHER_KEY="COURSE_TEACHER_KEY";
    public static final String COURSE_TEACHER_BY_COURSE_KEY="TEACHER_COURSE_KEY";
    public static final String COURSE_STUDENT_BY_COURSE_KEY="STUDENT_COURSE_KEY";

    public static final String COURSE_STUDENT_KEY="COURSE_STUDENT_KEY";

    public static final String SESSION_CURRENT_USER_KEY = "currentLoginUser";
    public static final String COURSETOPIC_BY_NAME = "COURSETOPIC_BY_NAME";
    public static final String COURSETOPIC_BY_COURSE = "COURSETOPIC_BY_COURSE";
    public static final String COURSETOPIC_BY_ID = "COURSETOPIC_BY_ID";

    public static final String COURSETOPICITEM_BY_TOPIC_ID="COURSETOPICITEM_BY_TOPIC_ID";
    public static final String COURSETOPICITEM_BY_ITEM_ID="COURSETOPICITEM_BY_ITEM_ID";

    public static String joinKey(Object object){
        String str=null;
        if (object instanceof CourseKey){
            CourseKey obj = (CourseKey) object;
            str = StringUtils.join(
                    new String[]{
                            COURSE_KEY,
                            obj.getCourseId(),
                            obj.getCourseGroup().toString(),
                            obj.getUniId()
            },":");
        }else if (object instanceof CourseTeacherKey){
            CourseTeacherKey obj = (CourseTeacherKey) object;
            str = StringUtils.join(
                    new String[]{
                            COURSE_TEACHER_KEY,
                            obj.getUserId().toString(),
                    },":");
        }else if (object instanceof CourseTopic){
            CourseTopic obj = (CourseTopic) object;
            str = StringUtils.join(
                    new String[]{
                            COURSETOPIC_BY_COURSE,
                            obj.getUniId(),
                            obj.getCourseId(),
                            obj.getCourseGroup().toString()
                    },":");
        }
        return str;
    }



    public static String returnUserRedisKeyByUserId(Integer userId){
        String str=null;
        str = StringUtils.join(new String[]{
                USER_KEY,
                userId.toString()
        },":");
        return str;
    }

    public static String returnCurrentUserRedisKeyByUserId(Integer userId){
        String str=null;
        str = StringUtils.join(new String[]{
                CURRENT_USER_PRI_KEY,
                userId.toString()
        },":");
        return str;
    }

    public static String returnCrouseKeyPath(CourseTopic courseTopic){
        String str = StringUtils.join(
                new String[]{
                        courseTopic.getTopicId().toString(),
                        courseTopic.getUniId(),
                        courseTopic.getCourseId(),
                        courseTopic.getCourseGroup().toString()
                }, "/");
        return str;
    }
}

